//
//  BoatDetailViewController.m
//  shuiyun
//
//  Created by 徐双 on 2017/9/6.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "BoatDetailViewController.h"
#import "BoatBeizhuViewCell.h"
#import "BoatDetailViewCell.h"

@interface BoatDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong)NSMutableDictionary * dict;

@property(nonatomic,strong)UILabel * label2;

@end

@implementation BoatDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     [self setUpBoatDetail];
    [self setTitle:@"船源信息详情"];
    //_dict = [NSMutableDictionary dictionary];
    [self setUpFirstView];
    [self setUpTableview];
    
   
    
}

-(void)setUpFirstView{

    UILabel * label1 = [[UILabel alloc] init];
    [self.view addSubview:label1];
    label1.text = @"发布信息:";
    label1.font = [UIFont systemFontOfSize:17];
    label1.textColor = [UIColor colorWithRed:129/255.0f green:129/255.0f blue:129/255.0f alpha:1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(80);
        make.top.equalTo(self.view).with.offset(5);
        make.width.offset(80);
        make.height.offset(25);
        
    }];
    
    self.label2 = [[UILabel alloc] init];
    [self.view addSubview:self.label2];
    
    self.label2.font = [UIFont systemFontOfSize:17];
    self.label2.textColor = [UIColor colorWithRed:129/255.0f green:129/255.0f blue:129/255.0f alpha:1];
    [self.label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(label1.mas_right).with.offset(5);
        make.centerY.equalTo(label1.mas_centerY);
        make.width.offset(400);
        make.height.offset(25);
        
    }];

}

-(void)setUpTableview{

    
    //初始化tableView
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 35, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 35);
    
    
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.estimatedRowHeight = 800;
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    
    
    [self.view addSubview:self.tableView];
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BoatBeizhuViewCell" bundle:nil] forCellReuseIdentifier:@"BoatBeizhuViewCell"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BoatDetailViewCell" bundle:nil] forCellReuseIdentifier:@"BoatDetailViewCell"];
}

-(void)setUpBoatDetail{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    
  
    NSString *token = [PublicFunction getConfigWithKey:@"token"];
    [productParams setValue:token forKey:@"token"];
    NSString * userid = [PublicFunction getConfigWithKey:@"userid"];
    [productParams setValue:userid forKey:@"userid"];
    //NSString * id = [PublicFunction getConfigWithKey:@"id"];
    [productParams setValue:self.idStr forKey:@"id"];
    
    
    [[MNetworkHandler sharedInstance]conURL:boatMessageDetail networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
        NSLog(@"%@", returnData);
        NSLog(@"%@", productParams);
        
        if ([[returnData objectForKey:@"code"]intValue]==1) {
            
            _dict = [returnData objectForKey:@"data"];
            NSLog(@"!!!!!!!!!!!!!!!%@",_dict);
            self.label2.text = [_dict objectForKey:@"s_time"];
            [self.tableView reloadData];
            
            [hud removeFromSuperview];
        }
        
        else {
            
            [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
            
            [hud removeFromSuperview];
        }
        
    } failureBlock:^(NSError *error) {
        
        NSLog(@"%@",error);
        
        [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
        
        [hud removeFromSuperview];        
    }];
    
}

#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 8;
}

#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 8;
    }else{
    
        return 3;
    
    }
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        return 50;
    }else{
    
    
        if (indexPath.row == 2) {
            
            return UITableViewAutomaticDimension;
        }
    }
    
    return 50;
    
}

#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        
        BoatDetailViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BoatDetailViewCell"];
        cell.selectionStyle = UITableViewCellEditingStyleNone;
        if (_dict) {
            
        }
        if (indexPath.row == 0) {
            
            cell.nameLabel.text = @"名称";
            
           // NSMutableDictionary *dic = self.dataArr[indexPath.row];
            cell.contentLabel.text = [_dict objectForKey:@"title"];
            
        }else if (indexPath.row == 1){
        
            cell.nameLabel.text = @"空船期限";
            
            //NSMutableDictionary *dic = self.dataArr[indexPath.row];
            cell.contentLabel.text = [_dict objectForKey:@"free"];
            
        
        }else if (indexPath.row == 2){
            
            cell.nameLabel.text = @"有效期";
            cell.contentLabel.text = [_dict objectForKey:@"valid"];
            
        }else if (indexPath.row == 3){
            
            cell.nameLabel.text = @"所在地";
            cell.contentLabel.text = [_dict objectForKey:@"start"];
            
        }else if (indexPath.row == 4){
            
            cell.nameLabel.text = @"到达地";
            cell.contentLabel.text = [_dict objectForKey:@"end"];
            
        }else if (indexPath.row == 5){
            
            cell.nameLabel.text = @"航线类别";
            cell.contentLabel.text = [_dict objectForKey:@"type"];

            
        }else if (indexPath.row == 6){
            
            cell.nameLabel.text = @"重量";
            cell.contentLabel.text = [_dict objectForKey:@"tonnage"];

            
        }else{
        
            cell.nameLabel.text = @"船舶种类";
            cell.contentLabel.text = [_dict objectForKey:@"shiptype"];
        }
        return cell;
        
    }else{
    
        BoatDetailViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BoatDetailViewCell"];
            cell.selectionStyle = UITableViewCellEditingStyleNone;
        if (indexPath.row == 0) {
            
            cell.nameLabel.text = @"联系人";
            cell.contentLabel.text = [_dict objectForKey:@"linkman"];

        }else if (indexPath.row == 1){
        
            cell.nameLabel.text = @"联系方式";
            cell.contentLabel.text = [_dict objectForKey:@"phone"];

        }else{
        
            BoatBeizhuViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BoatBeizhuViewCell"];
            cell.selectionStyle = UITableViewCellEditingStyleNone;
            
            cell.beizhuLabel.text = @"备注";
            cell.beizhuContentLabel.text = [_dict objectForKey:@"comment"];
            [cell.beizhuContentLabel sizeToFit];
            return cell;
        }
        
        return cell;
    }
    
    
}

-(NSMutableDictionary *)dict{

    if (!_dict) {
        
        _dict = [NSMutableDictionary dictionary];
    }

    return _dict;
}


@end
